<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\UserBalance;
use app\models\UserStore;
use app\models\UserStoreLog;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award
{
    protected $user;
    protected $order;
    protected $set;

    protected $items;

    // 总金额
    protected $amount;

    // 复购商品单价
    protected $price;

    // 复购数量
    protected $box;

    protected $is_bao = false;
    protected $is_repeat = false;

    // 是否为重算订单
    protected $is_reset = false;


    public function __construct(Order $order, $is_reset = false)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $order->user_id]);
        $this->set = SetUserUp::getLastOne();

        $this->is_reset = $is_reset;

        $this->items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 复购商品价格
        $this->price = Product::find()
            ->select(['price'])
            ->where(['id' => $product_id])
            ->scalar();

        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 报单区
        $this->is_bao = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT']);
        if ($this->is_bao) {
            $this->bao();
        }

        // 复购区
        $this->is_repeat = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPEAT']);
        if ($this->is_repeat) {
            $this->order->is_repeat = 1;
            $this->order->save();
            $this->repeat();
        }


    }

    // 报单
    public function bao()
    {
        $r = Chain::getReferrer($this->user->id, true);
        if (!$r || $r->level < UserLevel::$keys['vip1']) {
            return;
        }

        $name1 = '推荐奖';
        $type1 = 11;
        $name2 = '间荐奖';
        $type2 = 12;

        if ($this->amount == $this->set->vip1) {
            if ($r->level >= UserLevel::$keys['vip2']) {
                $amount = $this->set->r1 + $this->set->r2;
                $this->saveCommission($r->id, $amount, $type1, $name1);
                $this->equal($r->id, $amount);
            } else if ($r->level == UserLevel::$keys['vip1']) {
                $this->saveCommission($r->id, $this->set->r1, $type1, $name1);
                $r2 = $this->getIndirect($r->id);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->r2, $type2, $name2);
                    $this->equal($r2->id, $this->set->r2);
                }
            }
        }
    }

    // 平级奖
    public function equal($user_id, $amount)
    {
        $name = '平级奖';
        $type = 20;

        $chain = Chain::getReferrerChain($user_id, [
            UserLevel::$keys['vip2'],
            UserLevel::$keys['svip1'],
            UserLevel::$keys['svip2'],
            UserLevel::$keys['svip3'],
        ], true);
        if ($chain) {
            $user = $chain[0];

            $commission = $amount * $this->set->e / 100;
            $this->saveCommission($user->id, $commission, $type, $name);
        }
    }

    // 复购
    // public function repeat()
    // {
    //     // 上次有库存, 扣上级的, 否则从平台走
    //     $r = Chain::getReferrer($this->user->id);
    //     $box = 0;
    //     foreach ($this->items as $item) {
    //         $box += $item['quantity'];
    //     }
    //     $this->box = $box;
    //     // var_dump($box);exit;
    //
    //     if ($r) {
    //         $store = UserStore::find()
    //             ->where(['user_id' => $r->id])
    //             ->one();
    //         if ($store && intval($store->num) >= $box) {
    //             $diff = $this->diff($r);
    //
    //             // 减云仓
    //             if (!$this->is_reset) {
    //                 UserStoreLog::addLog($r->id, $store->num, -$box, $store->num - $box,
    //                     '出货', 2, $this->order->id);
    //                 $store->num -= $box;
    //                 $store->save();
    //             }
    //             $this->saveCommission($r->id, $this->amount - $diff, 31, '出货');
    //
    //             // 非平台出货
    //             $this->order->is_platform = 0;
    //             $this->order->save();
    //
    //         } else {
    //             $this->diff();
    //         }
    //     } else {
    //         $this->diff();
    //     }
    // }

    // 复购差价
    // 指定的用户, 不拿差价, 直接拿出货价
    // public function diff(User $the_user = null)
    // {
    //     $sum = 0;
    //
    //     $name = '差价';
    //     $type = 30;
    //
    //     $h_1 = false;
    //     $h_2 = false;
    //     $h_3 = false;
    //     $h_4 = false;
    //     $h_5 = false;
    //     $h_6 = false;
    //     $h_7 = false;
    //     $h_8 = false;
    //     $h_9 = false;
    //     $h_10 = false;
    //
    //     $h_11 = false;
    //     $h_12 = false;
    //
    //     $h_21 = false;
    //     $h_22 = false;
    //     $h_23 = false;
    //     $h_24 = false;
    //     $h_25 = false;
    //     $h_26 = false;
    //     $h_27 = false;
    //
    //     $d_1 = 0;
    //     $d_2 = 0;
    //     $d_3 = 0;
    //     $d_4 = 0;
    //     $d_5 = 0;
    //     $d_6 = 0;
    //     $d_7 = 0;
    //     $d_8 = 0;
    //     $d_9 = 0;
    //     $d_10 = 0;
    //
    //     $d_11 = 0;
    //     $d_12 = 0;
    //
    //     $d_21 = 0;
    //     $d_22 = 0;
    //     $d_23 = 0;
    //     $d_24 = 0;
    //     $d_25 = 0;
    //     $d_26 = 0;
    //     $d_27 = 0;
    //
    //     $chain = Chain::getReferrerChain($this->user->id, [
    //         UserLevel::$keys['vip1'],
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4'],
    //         UserLevel::$keys['vip5'],
    //         UserLevel::$keys['vip6'],
    //         UserLevel::$keys['vip7'],
    //         UserLevel::$keys['vip8'],
    //         UserLevel::$keys['vip9'],
    //         UserLevel::$keys['vip10'],
    //
    //         // 加上两个新等级
    //         UserLevel::$keys['vip11'],
    //         UserLevel::$keys['vip12'],
    //
    //         // 这里加上了运营中心, 区代,
    //         UserLevel::$keys['svip1'],
    //         UserLevel::$keys['svip2'],
    //         UserLevel::$keys['svip3'],
    //
    //         UserLevel::$keys['svip4'],
    //         UserLevel::$keys['svip5'],
    //         UserLevel::$keys['svip6'],
    //         UserLevel::$keys['svip7'],
    //     ], true);
    //
    //     foreach ($chain as $user) {
    //         if ($user->level <= $this->user->level) {
    //             continue;
    //         }
    //
    //         if ((!$h_1 && !$h_2 && !$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip1']) {
    //             // $d_1 = $this->_diff($this->set->re1);
    //             $d_1 = $this->_diff('r1');
    //             $h_1 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_1, $type, $name);
    //                 $sum += $d_1;
    //             }
    //         }
    //         if ((!$h_2 && !$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip2']) {
    //             // $d_2 = $this->_diff($this->set->re2);
    //             $d_2 = $this->_diff('r2');
    //             $d_2 -= $d_1;
    //             $h_2 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_2, $type, $name);
    //                 $sum += $d_2;
    //             }
    //         }
    //         if ((!$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip3']) {
    //             // $d_3 = $this->_diff($this->set->re3);
    //             $d_3 = $this->_diff('r3');
    //             $d_3 -= $d_2 + $d_1;
    //             $h_3 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_3, $type, $name);
    //                 $sum += $d_3;
    //             }
    //         }
    //         if ((!$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip4']) {
    //             // $d_4 = $this->_diff($this->set->re4);
    //             $d_4 = $this->_diff('r4');
    //             $d_4 -= $d_2 + $d_3 + $d_1;
    //             $h_4 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_4, $type, $name);
    //                 $sum += $d_4;
    //             }
    //         }
    //         if ((!$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip5']) {
    //             // $d_5 = $this->_diff($this->set->re5);
    //             $d_5 = $this->_diff('r5');
    //             $d_5 -= $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_5 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_5, $type, $name);
    //                 $sum += $d_5;
    //             }
    //         }
    //         if ((!$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip6']) {
    //             // $d_6 = $this->_diff($this->set->re6);
    //             $d_6 = $this->_diff('r6');
    //             $d_6 -= $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_6 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_6, $type, $name);
    //                 $sum += $d_6;
    //             }
    //         }
    //         if ((!$h_7 && !$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip7']) {
    //             // $d_7 = $this->_diff($this->set->re7);
    //             $d_7 = $this->_diff('r7');
    //             $d_7 -= $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_7 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_7, $type, $name);
    //                 $sum += $d_7;
    //             }
    //         }
    //         if ((!$h_8 && !$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip8']) {
    //             // $d_8 = $this->_diff($this->set->re8);
    //             $d_8 = $this->_diff('r8');
    //             $d_8 -= $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_8 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_8, $type, $name);
    //                 $sum += $d_8;
    //             }
    //         }
    //         if ((!$h_9 && !$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip9']) {
    //             // $d_9 = $this->_diff($this->set->re9);
    //             $d_9 = $this->_diff('r9');
    //             $d_9 -= $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_9 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_9, $type, $name);
    //                 $sum += $d_9;
    //             }
    //         }
    //         if ((!$h_10
    //                 && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip10']) {
    //             // $d_10 = $this->_diff($this->set->re10);
    //             $d_10 = $this->_diff('r10');
    //             $d_10 -= $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_10 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_10, $type, $name);
    //                 $sum += $d_10;
    //             }
    //             // break;
    //         }
    //
    //         if ((!$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip11']) {
    //             $d_11 = $this->_diff('r11');
    //             $d_11 -= $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_11 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_11, $type, $name);
    //                 $sum += $d_11;
    //             }
    //         }
    //         if ((!$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip12']) {
    //             $d_12 = $this->_diff('r12');
    //             $d_12 -= $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_12 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_12, $type, $name);
    //                 $sum += $d_12;
    //             }
    //         }
    //
    //         if ((!$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip1']) {
    //             $d_21 = $this->_diff('r101');
    //             $d_21 -= $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_21 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_21, $type, $name);
    //                 $sum += $d_21;
    //             }
    //         }
    //         if ((!$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip2']) {
    //             $d_22 = $this->_diff('r102');
    //             $d_22 -= $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_22 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_22, $type, $name);
    //                 $sum += $d_22;
    //             }
    //         }
    //         if ((!$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip3']) {
    //             $d_23 = $this->_diff('r103');
    //             $d_23 -= $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_23 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_23, $type, $name);
    //                 $sum += $d_23;
    //             }
    //         }
    //
    //         if ((!$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip4']) {
    //             $d_24 = $this->_diff('r201');
    //             $d_24 -= $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_24 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_24, $type, $name);
    //                 $sum += $d_24;
    //             }
    //         }
    //         if ((!$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip5']) {
    //             $d_25 = $this->_diff('r202');
    //             $d_25 -= $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_25 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_25, $type, $name);
    //                 $sum += $d_25;
    //             }
    //         }
    //         if ((!$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip6']) {
    //             $d_26 = $this->_diff('r203');
    //             $d_26 -= $d_25 + $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_26 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_26, $type, $name);
    //                 $sum += $d_26;
    //             }
    //         }
    //         if ((!$h_27) && $user->level == UserLevel::$keys['svip7']) {
    //             $d_27 = $this->_diff('r204');
    //             $d_27 -= $d_26 + $d_25 + $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
    //             $h_27 = true;
    //             if (!$the_user || ($the_user && $the_user->id != $user->id)) {
    //                 $this->saveCommission($user->id, $d_27, $type, $name);
    //                 $sum += $d_27;
    //             }
    //             break;
    //         }
    //     }
    //
    //     return $sum;
    // }

    // 计算差价(打折)
    // protected function _diff($set)
    // {
    //     // 支付金额 - 折扣价 * 数量
    //     $amount = $this->amount - ($set / 10 * $this->price) * $this->box;
    //
    //     return $amount;
    // }

    public function repeat()
    {
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['svip1'],
            UserLevel::$keys['svip2'],
            UserLevel::$keys['svip3'],
        ], true);

        $award_name = '差价';
        $award_type = 3;

        $has_vip1 = false;
        $has_vip2 = false;
        $has_svip1 = false;
        $has_svip2 = false;
        $has_svip3 = false;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_svip1 = 0;
        $diff_svip2 = 0;
        $diff_svip3 = 0;

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                continue;
            }

            if ((!$has_vip1 && !$has_vip2 && !$has_svip1 && !$has_svip2 && !$has_svip3) && $user->level == UserLevel::$keys['vip1']) {
                $diff_vip1 = $this->_diff('vip1');
                $this->saveCommission($user->id, $diff_vip1, $award_type, $award_name);
                $has_vip1 = true;
            }
            if ((!$has_vip2 && !$has_svip1 && !$has_svip2 && !$has_svip3) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->_diff('vip2');
                $diff_vip2 -= $diff_vip1;
                $this->saveCommission($user->id, $diff_vip2, $award_type, $award_name);
                $has_vip2 = true;
            }
            if ((!$has_svip1 && !$has_svip2 && !$has_svip3) && $user->level == UserLevel::$keys['svip1']) {
                $diff_svip1 = $this->_diff('svip1');
                $diff_svip1 -= $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_svip1, $award_type, $award_name);
                $has_svip1 = true;
            }
            if ((!$has_svip2 && !$has_svip3) && $user->level == UserLevel::$keys['svip2']) {
                $diff_svip2 = $this->_diff('svip2');
                $diff_svip2 -= $diff_svip1 + $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_svip2, $award_type, $award_name);
                $has_svip2 = true;
            }
            if ((!$has_svip3) && $user->level == UserLevel::$keys['svip3']) {
                $diff_svip3 = $this->_diff('svip3');
                $diff_svip3 -= $diff_svip2 + $diff_svip1 + $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_svip3, $award_type, $award_name);
                $has_svip3 = true;
                break;
            }
        }
    }

    // 这个是最新的差价
    protected function _diff($diff_key)
    {
        $order_items = $this->items;

        $sum = 0;
        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();

            // var_dump($product);exit;

            $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
        }

        // 用订单价 - 复购价 = 差价
        return $sum;

    }

    // 2 + 1 上级
    private function getIndirect($user_id)
    {
        $the_referrer = Chain::getReferrer($user_id, true);
        if (!$the_referrer) {
            return null;
        }

        // 高等级
        // if ($the_referrer->level >= UserLevel::$keys['svip1']) {
        //     // 她的不截断
        //     // return $the_referrer;
        // }

        // 处理两线条
        if ($the_referrer->level >= UserLevel::$keys['vip2']) {

            // 判断是否属于前2个直推
            $children = Chain::getChildrenSort($the_referrer->id, ['id']);

            // if (count($children) > $this->set->vip2) {
            if (count($children) > 2) {
                $children = [$children[0], $children[1]];
                $in = in_array($user_id, $children);
                if (!$in) {
                    return $the_referrer;
                } else {
                    return $this->getIndirect($the_referrer->id);
                }
            } else {
                return $this->getIndirect($the_referrer->id);
            }
        }

        return $this->getIndirect($the_referrer->id);
    }

    // 保存佣金 (await balance)
    protected function saveCommission($user_id, $balance, $type, $name, $status = 1)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = $status;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();
            $account = UserAccount::findOne(['user_id' => $user_id]);

            if ($account) {
                if ($status == 1) {
                    $account->balance += $balance;
                } else {
                    $account->await_balance += $balance;
                }

              $account->save();
            }

            // balance logs
            if ($status == 1) {
                UserBalance::addLog($model->user_id,
                    $account->balance, $model->balance, $account->balance + $model->balance, '返佣', $this->order->id);
            }

            return true;
        }
    }

}